package com.ctaa.ctaa.utils;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class StrongPasswordGenerator {
    private static final String LOWER_CASE = "abcdefghijklmnopqrstuvwxyz";
    private static final String UPPER_CASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    private static final String DIGITS = "0123456789";
    private static final SecureRandom random = new SecureRandom();

    public static String generatePassword() {
        // 确保密码包含至少一个小写字母、一个大写字母和一个数字
        List<Character> passwordChars = new ArrayList<>();
        passwordChars.add(getRandomChar(LOWER_CASE));
        passwordChars.add(getRandomChar(UPPER_CASE));
        passwordChars.add(getRandomChar(DIGITS));

        // 生成剩余的字符
        String allChars = LOWER_CASE + UPPER_CASE + DIGITS;
        for (int i = 3; i < 8; i++) {
            passwordChars.add(getRandomChar(allChars));
        }

        // 打乱字符顺序
        Collections.shuffle(passwordChars, random);

        // 转换为字符串
        StringBuilder password = new StringBuilder();
        for (char c : passwordChars) {
            password.append(c);
        }

        return password.toString();
    }

    private static char getRandomChar(String chars) {
        return chars.charAt(random.nextInt(chars.length()));
    }

    public static void main(String[] args) {
        // 生成10个随机密码用于测试
        for (int i = 0; i < 10; i++) {
            System.out.println("生成的随机密码: " + generatePassword());
        }
    }
}
